package com.canyou.utils;

import com.canyou.exception.CysBusinessException;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 高德地图web服务接口
 *
 * @author fy
 * @date 20/02/27 17:34
 */
@Component
public final class AmapUtils {

    private static final String KEY = "e2053f333aa78f0ab3e650266e9db4c4";
    private static final String GEO_URL = "https://restapi.amap.com/v3/geocode/geo?key={key}&address={address}&city={city}&batch={batch}";

    private static final RestTemplate REST_TEMPLATE = new RestTemplate();

    /**
     * 根据地址信息获取坐标
     *
     * @param address 地址
     * @param city    城市
     * @return
     */
    public Float[] geoLngLat(String address, String city) {
        AmapRes res = geo(address, city);
        if (!String.valueOf(1).equals(res.getStatus())) {
            throw new CysBusinessException(Integer.valueOf(res.getInfocode()), res.getInfo());
        }
        String location = res.getGeocodes().get(0).getLocation();
        String[] sLngLat = location.split(",");
        Float[] lngLat = new Float[]{Float.parseFloat(sLngLat[0]), Float.parseFloat(sLngLat[1])};
        return lngLat;
    }

    /**
     * 调用高德地理编码接口
     * 返回的数据格式如下:
     * <pre>
     * {
     *    "count" : "1",
     *    "geocodes" : [
     *       {
     *          "adcode" : "440309",
     *          "building" : {
     *             "name" : [],
     *             "type" : []
     *          },
     *          "city" : "深圳市",
     *          "citycode" : "0755",
     *          "country" : "中国",
     *          "district" : "龙华区",
     *          "formatted_address" : "广东省深圳市龙华区龙悦居二期北门",
     *          "level" : "兴趣点",
     *          "location" : "114.029531,22.602973",
     *          "neighborhood" : {
     *             "name" : [],
     *             "type" : []
     *          },
     *          "number" : [],
     *          "province" : "广东省",
     *          "street" : [],
     *          "township" : []
     *       }
     *    ],
     *    "info" : "OK",
     *    "infocode" : "10000",
     *    "status" : "1"
     * }
     * </pre>
     *
     * @param address 地址
     * @param city    城市
     * @return
     */
    public AmapRes geo(String address, String city) {
        return geo(address, city, false);
    }

    private AmapRes geo(String address, String city, boolean batch) {
        Map<String, Object> param = new HashMap<>(4);
        param.put("key", KEY);
        param.put("address", address);
        if (StringUtils.isNotBlank(city)) {
            param.put("city", city);
        }
        param.put("batch", batch);
        AmapRes result = REST_TEMPLATE.getForObject(GEO_URL, AmapRes.class, param);
        return result;
    }

    @Data
    @NoArgsConstructor
    public static class AmapRes {
        private String status;
        private String infocode;
        private String info;
        private String count;
        private List<Geocode> geocodes;
    }

    @Data
    @NoArgsConstructor
    public static class Geocode {
        private String adcode;
        private String country;
        private String province;
        private String city;
        private String district;
        private String citycode;
        private String formatted_address;
        private String level;
        private String location;
    }

}
